﻿Imports System.Data.SqlClient

Public Class 数据字典_支出方式设置
    Private Sub 退出btn_Click(sender As Object, e As EventArgs) Handles 退出btn.Click
        Me.Close()
    End Sub

    Private Sub 刷新btn_Click(sender As Object, e As EventArgs) Handles 刷新btn.Click
        GetzffsData()
    End Sub

    Private Function GetzffsData()
        Dim sql As String = "
                    SELECT 
                      ID AS '编号',
                      ZCFS AS '支付方式',
                      CJSJ AS '创建时间',
                      ZFZH AS '支付账号',
                      MEMO AS '备注',
                      JLZT AS '记录状态'
                    FROM ZCFSK
                    ORDER BY ID
                    "
        DbConnectionHelper.FillDataGridView(支付方式Datagridview, sql)
    End Function

    Private Sub 数据字典_支出方式设置_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GetzffsData()
    End Sub

    Private Sub 清空btn_Click(sender As Object, e As EventArgs) Handles 清空btn.Click
        支付方式_data.Clear()
        账号_data.Clear()
        备注_data.Clear()
    End Sub

    Private Sub 保存_Click(sender As Object, e As EventArgs) Handles 保存.Click
        ' 1️⃣ 非空校验
        If String.IsNullOrWhiteSpace(支付方式_data.Text) Then
            MessageBox.Show("支付方式不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            支付方式_data.Focus()
            Exit Sub
        End If

        If String.IsNullOrWhiteSpace(账号_data.Text) Then
            MessageBox.Show("账号不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            账号_data.Focus()
            Exit Sub
        End If

        ' 备注可以为空，不做强制校验

        ' 2️⃣ 构建SQL语句
        Dim sql As String = "INSERT INTO ZCFSK (ZCFS, ZFZH, MEMO) VALUES (@ZCFS, @ZFZH, @MEMO)" ' 注意补上 JLZT 的值

        ' 3️⃣ 执行数据库插入
        Try
            Using conn As SqlConnection = DbConnectionHelper.Connection
                DbConnectionHelper.OpenConnection(conn)

                Using cmd As New SqlCommand(sql, conn)
                    cmd.Parameters.AddWithValue("@ZCFS", 支付方式_data.Text.Trim())
                    cmd.Parameters.AddWithValue("@ZFZH", 账号_data.Text.Trim())
                    cmd.Parameters.AddWithValue("@MEMO", 备注_data.Text.Trim())

                    Dim rowsAffected As Integer = cmd.ExecuteNonQuery()
                    If rowsAffected > 0 Then
                        MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Else
                        MessageBox.Show("保存失败，没有插入任何数据。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    End If
                End Using

                DbConnectionHelper.CloseConnection(conn)
            End Using
        Catch ex As Exception
            MessageBox.Show("保存失败：" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class